import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Cuop extends sprite {

	public int weight;
	public int mstate;
	public int xspeed = 5;
	public int vy;
	public int vx = 0;
	public static int xc,yc;
	public static int state_stand = 0;
	public static int state_sea = 1;
	public static int state_evil = 2;
	public static int state_power = 3;
	
	public static int mDirection;
	public static int direc_fly = 0;
	public static int direc_left = 1;
	public static int direc_right = 2;
	
	public static  int mlife = 100;

	public static int cmX, cmY, cmvx, cmvy, cmdx, cmdy;
	public static int cmtoX, cmtoY;
	public static int cmOx;
	public static int cmOt ;
	public static int sx,sxe,sw,sy,sye, sh;

	public static Vector effectsVec = new Vector();
	public static Vector vecboom = new Vector();

	public Cuop(int x, int y, String path) {
		super(x, y, path);
		// TODO Auto-generated constructor stub
		mstate = state_stand;
	}

	public void draw(Graphics g) {
		
		g.setColor(0xFFFFFF);			
		for (int i = (cmX/2) % 40; i < Mscreen.widthScreen + 40; i+=40) {		
			g.drawImage(BackG.bk[4], cmX + Mscreen.widthScreen - i, backGY(),Graphics.BOTTOM|Graphics.LEFT);
		}
		for (int i = (cmX ) % 80; i < Mscreen.widthScreen + 80; i+=80) {
			g.drawImage(BackG.bk[5], cmX + Mscreen.widthScreen - i, backGY(),Graphics.BOTTOM|Graphics.LEFT);
		}
		
		super.draw(g);		

		
		g.setColor(0,255,255);
		g.drawString("Blood : " + Global.blood, cmX, 0, 20);
		g.drawString("Score : " + Global.score, cmX + Mscreen.widthScreen -60, 0, 20);
		g.drawString("Level : " + Level.currentLevel, cmX + Mscreen.widthScreen/2, 0, 20);
	}
	
	public static int backGY()
	{
		int yy ;
//		int count= 0;
//		for(int i = Mscreen.heightScree; i>=0; i-= TileMap.size)
//		{
//			for(int j = Cuop.cmX ; j <Cuop.cmX+Mscreen.widthScreen; j+= TileMap.size)
//			{					
//				int t = TileMap.getCurrentmapFoot(i, j);
//				if(TileMap.testTileStand(t) || t == 22)
//				{			
//					Global.arrayYB[count] = i + TileMap.lech ;				
//					 count++;	
//				}
//							
//			}
//		}
//		
//		int max = Global.arrayYB[0];
//		for(int f=1; f<Global.arrayYB.length; f++)
//		{
//			if(Global.arrayYB[f] > max)
//			{
//				max = Global.arrayYB[f];
//			}
//		}
//				
//		yy = max;
		
		yy = Mscreen.heightScree - (4*TileMap.size+16);
		return yy;
	}
	

	public void update() {
		updateKey();
		chuyencamera();
		currentY += vy;
		
		if(mDirection==direc_left)
		{
			if( vx!=0)
			{
				vx ++;				
				System.out.println(vx);
			}
		}
		else if(mDirection == direc_right)
		{
			if( vx!=0)
			{
				vx --;				
				System.out.println(vx);
			}
		}
		
		
		//System.out.println("state : " + mstate);
		if(mstate == state_power )
		{
			mlife--;
			if(mlife==0)
			{				
				mstate = state_stand;
				mlife = 100;
			}

		}	
		
		Global.cuopX = currentX;
		Global.cuopY = currentY;
		Global.cuopW = width;
		Global.cuopH = height;
	}

	public void updateKey() {
		oldX = currentX;
		
		if(mstate == state_stand || mstate == state_power)
		{
			if (ExGameCanvas.keyPressed[4] || ExGameCanvas.keyHold[4] && isDraw) 
			{
				mDirection = direc_left;
				//if ( TileMap.getCurrentmapLeft(currentY + height / 2 - TileMap.lech,currentX) != 1)
				if(!TileMap.testTileLeft(TileMap.getCurrentmapLeft(currentY + height / 2 - TileMap.lech,currentX)))
				{
					if(currentX - xspeed > 0)
					{
						//currentX -= xspeed ;
						vx = -xspeed;
						currRow = 1;
					}
					currFrame = ++currFrame % COL;
				}
				
				if(ExGameCanvas.keyPressed[4])
				{
					ExGameCanvas.keyPressed[4] = false;
				}
			}
			
			

			if (ExGameCanvas.keyPressed[6] || ExGameCanvas.keyHold[6]&& isDraw)
			{
				mDirection = direc_right;
				//if (TileMap.getCurrentmapRight(currentY + height / 2 - TileMap.lech, currentX+ width) != 6 && TileMap.getCurrentmapRight(currentY + height / 2 - TileMap.lech, currentX+ width) != 1)
				if(!TileMap.testTileRight(TileMap.getCurrentmapRight(currentY + height / 2 - TileMap.lech, currentX+ width))
						&& !TileMap.testTileGoal(TileMap.getCurrentmapLeft(currentY + height / 2 - TileMap.lech,currentX)))
				{					
					//currentX += xspeed ;
					
					vx = xspeed;
					currRow = 2;
					currFrame = ++currFrame % COL;
				}
				else 
				{
					//if (TileMap.getCurrentmapRight(currentY + height / 2 - TileMap.lech, currentX+ width) == 21)
					if(TileMap.testTileGoal(TileMap.getCurrentmapRight(currentY + height / 2 - TileMap.lech, currentX+ width))
							|| (TileMap.testTileGoal(TileMap.getCurrentmapLeft(currentY + height / 2 - TileMap.lech,currentX))))
					{
						if (Level.currentLevel < 5 && (EvilBoss.mBoss<1)) 
						{
							Level.currentLevel++;
							Level.isNextLevel = true;
							RMS.savegame(Global.score);
							Game_src.removeEvils();
							ExGameCanvas.Mquaman.currentSreen();
						}

					}
				}
				if(ExGameCanvas.keyPressed[6])
				{					
					ExGameCanvas.keyPressed[6] = false;	
				}
			}
	

			if (ExGameCanvas.keyPressed[2]&& isDraw) 
			{
				//if (TileMap.getCurrentmapFoot(currentY + height- TileMap.lech, currentX+width/2) == 1 || currentY + height >= Mscreen.heightScree)
				if(TileMap.testTileStand(TileMap.getCurrentmapFoot(currentY + height- TileMap.lech, currentX+width/2)))
				{
		
					vy = -15;				
					//createEffect(Effect.KHOI);
					currRow = 3;
					currFrame = ++currFrame % COL;
				}
				ExGameCanvas.keyPressed[2] = false;
			}	
			
			if(ExGameCanvas.keyHold[4] && ExGameCanvas.keyPressed[8] && Boom.isdisappear )
			{
				createBoom(Boom.DAN,1);
				ExGameCanvas.clear_key();
			}
			
			if(ExGameCanvas.keyHold[6] && ExGameCanvas.keyPressed[8]&& Boom.isdisappear )
			{
				createBoom(Boom.DAN,2);
				ExGameCanvas.clear_key();
			}
			
			
		}

	

	}




	public void chuyencamera() {
		
		cmOx = Mscreen.widthScreen/2;
		cmOt = Global.maplenght - cmOx;
		sw = Mscreen.widthScreen/TileMap.size + 1;
		sh = Mscreen.heightScree / TileMap.size + 2;
		if(Mscreen.widthScreen % TileMap.size != 0)
		{
			sw += 1;
		}
		
		cmtoX = currentX - Mscreen.widthScreen / 2 +width ;
		cmtoY = 0;
		
		if (cmtoX < 0) {
			cmtoX = 0;
		}


		if (cmtoY > 0)
			cmtoY = 0;
		if (cmtoY < -70)
			cmtoY = -70;
		if (cmtoX!=cmX && cmtoX<(cmOt-cmOx + xspeed)) 
		{
			cmvx = (cmtoX - cmX) << 1;
			cmvy = (cmtoY - cmY) << 1;
			cmdx += cmvx;
			cmX += cmdx >> 4;
			cmdx = cmdx & 0xf;
			cmdy += cmvy;
			cmY += cmdy >> 4;
			cmdy = cmdy & 0xf;						
						
		}
		sx = cmX / TileMap.size;
		sxe = sx + sw;
		sy = cmY / TileMap.size;
		sye = sy + sh;
		if (sxe> TileMap.w) {
			sxe = TileMap.w;
		}
		if (sye > TileMap.h) {
			sye = TileMap.h;
		}

	}

	
	

	public void audrop() {
		
		if (TileMap.getCurrentmapFoot(currentY + height - TileMap.lech,currentX + width / 2) ==22)		
		{
			vy = 0;	
			createEffect(Effect.KHOI);
			isDraw = false;
			
			Global.blood -= 1 ;
			if(Global.blood>0)
			{				
				mstate = state_sea;
				Evils.evilVec.removeAllElements();
				//ExGameCanvas.restMap.currentSreen();
				Level.isResetLevel = true;
			}
			else if(Global.blood ==0)
			{
				isDraw = false;
				Level.isOver = true;
				Evils.evilVec.removeAllElements();
				ExGameCanvas.overMap.currentSreen();
			}
		}
		else
		{
			//if (TileMap.getCurrentmapFoot(currentY + height - TileMap.lech,currentX + width / 2) !=1 )
			if(!TileMap.testTileStand(TileMap.getCurrentmapFoot(currentY + height- TileMap.lech, currentX+width/2)))
			{
				if (currentY + height < Mscreen.heightScree) 
				{
					vy++;	
					//sprite.nextFrame();
					currRow = 3;
				}
				currentX +=vx;
			}
			//else if(TileMap.testTileStand(TileMap.getCurrentmapFoot(currentY + height- TileMap.lech, currentX+width/2)))
			else if(TileMap.testTileStand(TileMap.getCurrentmapFoot(currentY + height- TileMap.lech, currentX+width/2)))
			{
				vy = 0;
				currentX +=vx;

				if(Mscreen.heightScree <=200)
				{
					currentY = currentY / 32 * 32 + 20;
				}
				else if(Mscreen.heightScree >200)
				{
					currentY = currentY / 32 * 32 + 4;
				}
				currRow = 0;
			}
		}
	}



	public void createEffect(int type) {
		
		switch (type) {
		case Effect.KHOI:
			Effect khoi = new Effect(currentX + width / 2, currentY + height,Effect.KHOI);
			effectsVec.addElement(khoi);
			break;
		case Effect.MAU:
			Effect mau = new Effect(Boom.xD+Boom.wD/2, Boom.yD + Boom.hD/2,Effect.MAU);
			effectsVec.addElement(mau);
			break;
		default:
			break;
		}		
	}

	public void createBoom(int type1, int direc)
	{
		switch (type1) {
		case Boom.DAN:
			Boom bom = new Boom(currentX + width / 2, currentY + height/2,Boom.DAN,direc);
			vecboom.addElement(bom);
			break;

		default:
			break;
		}
	}
	
	
	public boolean isCollison(int x2, int y2, int w2, int h2) {
		boolean coll;
		if (((x2 <= currentX + width && x2 >= currentX) || (x2 + w2 >= currentX && x2
				+ w2 <= currentX + width))
				&& (y2 + h2 / 2 >= currentY && y2 + h2 / 2 <= currentY + height)) {
			coll = true;
		} else {
			coll = false;
		}

		return coll;
	}
	
	public boolean isCollisonSkill(int x2, int y2, int w2, int h2) 
	{
		boolean coll;
		if (((x2 <= currentX + width && x2 >= currentX) || (x2 + w2 >= currentX && x2 + w2 <= currentX + width))
				&& (currentY+height/2>=y2 && currentY+height/2<=y2+h2)) 
		{
			coll = true;					
		} 
		else 
		{
			coll = false;
		}

		return coll;
	}

	public void setPosActor(int level) {

		switch (level) {
		case 1:
			currentX = 0;
			currentY = 0;
			mstate = state_stand;
			break;
		case 2:
			currentX = 0;
			currentY = Mscreen.heightScree / 2;
			mstate = state_stand;
			break;

		case 3:
			currentX = 0;
			currentY = 0;
			mstate = state_stand;
			break;
		case 4 :
			currentX = 0;
			currentY = 0;
			mstate = state_stand;
			break;

		default:
			break;
		}
	}
	
	public void setReset(int level)
	{
		switch (level) {
		case 1:
			currentY = 0;
			mstate = state_power;
			break;
		case 2:
			currentY = 0;
			mstate = state_power;
			break;

		case 3:
			currentY = 0;
			mstate = state_power;
			break;
		case 4 :
			currentY = 0;
			mstate = state_power;
			break;

		default:
			break;
		}
	}
	


}
